/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * firstFeeDialog.java
 *
 * Created on 2011-5-24, 20:41:44
 */
package junchangmis.student;

import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import junchangmis.*;
import jxl.write.WriteException;
import publicmethod.*;
import java.sql.ResultSet;
import java.util.Vector;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;

/**
 *
 * @author developers
 */
public class firstFeeDialog extends javax.swing.JDialog {

    MainFrame parent;
    Vector<String> names = new Vector<String>();
    Vector<String> classCodes = new Vector<String>();
    Vector<String> sumFees = new Vector<String>();

    /** Creates new form firstFeeDialog */
    public firstFeeDialog(java.awt.Frame parent, boolean modal, MainFrame pparent) {
        super(parent, modal);
        this.parent = pparent;
        initComponents();
        this.setTitle("首次交费统计——选择要统计的时间段");
        this.setLocationRelativeTo(null);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxYearStart = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jComboBoxMonthStart = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jComboBoxDayStart = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jComboBoxYearEnd = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jComboBoxMonthEnd = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jComboBoxDayEnd = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jCheckBoxPrint = new javax.swing.JCheckBox();
        jButtonConfirm = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel1.setText("起始时间：");

        jLabel2.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel2.setText("终止时间：");

        jComboBoxYearStart.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jComboBoxYearStart.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));

        jLabel3.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel3.setText("年");

        jComboBoxMonthStart.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jComboBoxMonthStart.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        jLabel4.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel4.setText("月");

        jComboBoxDayStart.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jComboBoxDayStart.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        jLabel5.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel5.setText("日");

        jComboBoxYearEnd.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jComboBoxYearEnd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));

        jLabel6.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel6.setText("年");

        jComboBoxMonthEnd.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jComboBoxMonthEnd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        jLabel7.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel7.setText("月");

        jComboBoxDayEnd.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jComboBoxDayEnd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        jLabel8.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel8.setText("日");

        jCheckBoxPrint.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jCheckBoxPrint.setText("是否打印");

        jButtonConfirm.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jButtonConfirm.setText("确定");
        jButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxYearStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxMonthStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxDayStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBoxYearEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBoxMonthEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButtonConfirm)
                                .addComponent(jCheckBoxPrint)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxDayEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxYearStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBoxMonthStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBoxDayStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBoxYearEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBoxMonthEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jComboBoxDayEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addComponent(jCheckBoxPrint)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonConfirm)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmActionPerformed
        try {
            // TODO add your handling code here:
            DbOperation db = new DbOperation();
            db.DbConnect();
            String sql = "select * from formalStu where first='" + "true" + "'";
            ResultSet rs = db.DBSqlQuery(sql);
            while (rs.next()) {
                StringTokenizer st = new StringTokenizer(rs.getString("dateStart"), ".");
                Vector<String> temp = new Vector<String>();
                while (st.hasMoreTokens()) {
                    //System.out.println(st.nextToken());
                    temp.add(st.nextToken());
                }

                int yearStart = Integer.valueOf(this.jComboBoxYearStart.getSelectedItem().toString());
                int monthStart = Integer.valueOf(this.jComboBoxMonthStart.getSelectedItem().toString());
                int monthEnd = Integer.valueOf(this.jComboBoxMonthEnd.getSelectedItem().toString());

                int yearEnd = Integer.valueOf(this.jComboBoxYearEnd.getSelectedItem().toString());
                int dayStart = Integer.valueOf(this.jComboBoxDayStart.getSelectedItem().toString());
                int dayEnd = Integer.valueOf(this.jComboBoxDayEnd.getSelectedItem().toString());

                int y = Integer.valueOf(temp.get(0));
                int m = Integer.valueOf(temp.get(1));
                int d = Integer.valueOf(temp.get(2));

                if (yearStart <= y && y <= yearEnd) {
                    int start = (monthStart - 1) * 31 + dayStart;
                    int mid = (m - 1) * 31 + d;
                    int end = (monthEnd - 1) * 31 + dayEnd;

                    if (start <= mid && mid <= end) {
                        classCodes.add(rs.getString("classCode"));
                        names.add(rs.getString("name"));
                        sumFees.add(rs.getString("sumFee"));
                    }
                }
            }
            db.DbClose();
        } catch (SQLException ex) {
            Logger.getLogger(firstFeeDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (names.size() > 0) {//如果有查询结果
            try {
                //如果有查询结果
                String range = this.jComboBoxYearStart.getSelectedItem().toString() + "." + this.jComboBoxMonthStart.getSelectedItem().toString() + "." + this.jComboBoxDayStart.getSelectedItem().toString() + "-" + this.jComboBoxYearEnd.getSelectedItem().toString() + "." + this.jComboBoxMonthEnd.getSelectedItem().toString() + "." + this.jComboBoxDayEnd.getSelectedItem().toString();
                firstFeeExcel work = new firstFeeExcel(names, classCodes, sumFees, range);
                String filename = "初次交费统计表\\";
                filename = filename + range + ".xls";
                work.setOutputFile(filename);
                work.write();
                System.out.println("Please check the result file " + filename);
                JOptionPane.showConfirmDialog(null, "生成成功！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
                if (this.jCheckBoxPrint.isSelected() == true) {
                    PrintTest.print(PrintTest.getPath() + "\\" + filename);
                } else {
                    PrintTest.showTable(PrintTest.getPath() + "\\" + filename);
                }
                dispose();
            } catch (IOException ex) {
                Logger.getLogger(firstFeeDialog.class.getName()).log(Level.SEVERE, null, ex);
            } catch (WriteException ex) {
                Logger.getLogger(firstFeeDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            JOptionPane.showConfirmDialog(null, "没有记录！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButtonConfirmActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*  java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
        firstFeeDialog dialog = new firstFeeDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {

        public void windowClosing(java.awt.event.WindowEvent e) {
        System.exit(0);
        }
        });
        dialog.setVisible(true);
        }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JCheckBox jCheckBoxPrint;
    private javax.swing.JComboBox jComboBoxDayEnd;
    private javax.swing.JComboBox jComboBoxDayStart;
    private javax.swing.JComboBox jComboBoxMonthEnd;
    private javax.swing.JComboBox jComboBoxMonthStart;
    private javax.swing.JComboBox jComboBoxYearEnd;
    private javax.swing.JComboBox jComboBoxYearStart;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
